home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / deskbar-applet / handlers / galeon.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  12.5 KB  |  339 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import xml.sax as xml
  5. import traceback
  6. import re
  7. import urllib
  8. from os.path import join, expanduser, exists
  9. from gettext import gettext as _
  10. import gtk
  11. import gnomevfs
  12. import deskbar
  13. import deskbar.Indexer as deskbar
  14. import deskbar.Handler as deskbar
  15. from deskbar.Watcher import FileWatcher
  16. from deskbar.BrowserMatch import get_url_host, is_preferred_browser
  17. from deskbar.BrowserMatch import BrowserSmartMatch, BrowserMatch
  18. from deskbar.defs import VERSION
  19.  
  20. def _check_requirements():
  21.     if is_preferred_browser('galeon'):
  22.         return (deskbar.Handler.HANDLER_IS_HAPPY, None, None)
  23.     else:
  24.         return (deskbar.Handler.HANDLER_IS_NOT_APPLICABLE, 'Galeon is not your preferred browser, not using it.', None)
  25.  
  26. HANDLERS = {
  27.     'GaleonBookmarksHandler': {
  28.         'name': _('Web Bookmarks'),
  29.         'description': _('Open your web bookmarks by name'),
  30.         'requirements': _check_requirements,
  31.         'version': VERSION },
  32.     'GaleonHistoryHandler': {
  33.         'name': _('Web History'),
  34.         'description': _('Open your web history by name'),
  35.         'requirements': _check_requirements,
  36.         'version': VERSION },
  37.     'GaleonSearchHandler': {
  38.         'name': _('Web Searches'),
  39.         'description': _("Search the web via your browser's search settings"),
  40.         'requirements': _check_requirements,
  41.         'version': VERSION } }
  42. GALEON_HISTORY_FILE = expanduser('~/.galeon/history2.xml')
  43. if not exists(GALEON_HISTORY_FILE):
  44.     GALEON_HISTORY_FILE = expanduser('~/.galeon/history.xml')
  45.  
  46. GALEON_BOOKMARKS_FILE = expanduser('~/.galeon/bookmarks.xbel')
  47. favicon_cache = None
  48. bookmarks = None
  49. smart_bookmarks = None
  50. quicksearches = None
  51.  
  52. class GaleonHandler(deskbar.Handler.Handler):
  53.     
  54.     def __init__(self, watched_file, callback, icon = 'stock_bookmark'):
  55.         deskbar.Handler.Handler.__init__(self, icon)
  56.         self.watched_file = watched_file
  57.         self.watch_callback = callback
  58.  
  59.     
  60.     def initialize(self):
  61.         global favicon_cache
  62.         if not hasattr(self, 'watcher'):
  63.             self.watcher = FileWatcher()
  64.             self.watcher.connect(('changed',), (lambda watcher, f: self.watch_callback()))
  65.         
  66.         self.watcher.add(self.watched_file)
  67.         if favicon_cache == None:
  68.             favicon_cache = GaleonFaviconCacheParser().get_cache()
  69.         
  70.  
  71.     
  72.     def stop(self):
  73.         self.watcher.remove(self.watched_file)
  74.  
  75.  
  76.  
  77. class GaleonBookmarksHandler(GaleonHandler):
  78.     
  79.     def __init__(self):
  80.         GaleonHandler.__init__(self, (GALEON_BOOKMARKS_FILE,), (lambda : self._parse_bookmarks(True)))
  81.  
  82.     
  83.     def initialize(self):
  84.         GaleonHandler.initialize(self)
  85.         self._parse_bookmarks()
  86.  
  87.     
  88.     def _parse_bookmarks(self, force = False):
  89.         global bookmarks, smart_bookmarks, quicksearches
  90.         if force or bookmarks == None:
  91.             parser = GaleonBookmarksParser(self, favicon_cache)
  92.             bookmarks = parser.get_indexer()
  93.             smart_bookmarks = parser.get_smart_bookmarks()
  94.             quicksearches = parser.get_quicksearches()
  95.         
  96.  
  97.     
  98.     def query(self, query):
  99.         return bookmarks.look_up(query)[:deskbar.DEFAULT_RESULTS_PER_HANDLER]
  100.  
  101.  
  102.  
  103. class GaleonSearchHandler(GaleonBookmarksHandler):
  104.     
  105.     def __init__(self):
  106.         GaleonBookmarksHandler.__init__(self)
  107.  
  108.     
  109.     def query(self, query):
  110.         query_a = query.split(' ')
  111.         if len(query_a) > 1 and quicksearches.has_key(query_a[0]):
  112.             return (quicksearches[query_a[0]],)
  113.         else:
  114.             return smart_bookmarks
  115.  
  116.  
  117.  
  118. class GaleonHistoryHandler(GaleonHandler):
  119.     
  120.     def __init__(self):
  121.         GaleonHandler.__init__(self, GALEON_HISTORY_FILE, self._parse_history, 'epiphany-history.png')
  122.         self._history = None
  123.  
  124.     
  125.     def initialize(self):
  126.         GaleonHandler.initialize(self)
  127.         self._parse_history()
  128.  
  129.     
  130.     def _parse_history(self):
  131.         self._history = GaleonHistoryParser(self, favicon_cache).get_indexer()
  132.  
  133.     
  134.     def query(self, query):
  135.         return self._history.look_up(query)[:deskbar.DEFAULT_RESULTS_PER_HANDLER]
  136.  
  137.  
  138.  
  139. class GaleonBookmarksParser(xml.sax.ContentHandler):
  140.     
  141.     def __init__(self, handler, cache):
  142.         xml.sax.ContentHandler.__init__(self)
  143.         self.handler = handler
  144.         self.chars = ''
  145.         self.title = None
  146.         self.href = None
  147.         self.smarthref = None
  148.         self._indexer = deskbar.Indexer.Indexer()
  149.         self._qsindexer = { }
  150.         self._smart_bookmarks = []
  151.         self._cache = cache
  152.         self._index_bookmarks()
  153.  
  154.     
  155.     def get_indexer(self):
  156.         '''
  157. \t\tReturns a completed indexer with the contents of bookmark file
  158. \t\t'''
  159.         return self._indexer
  160.  
  161.     
  162.     def get_quicksearches(self):
  163.         return self._qsindexer
  164.  
  165.     
  166.     def get_smart_bookmarks(self):
  167.         '''
  168. \t\tReturn a list of GaleonSmartMatch instances representing smart bookmarks
  169. \t\t'''
  170.         return self._smart_bookmarks
  171.  
  172.     
  173.     def _index_bookmarks(self):
  174.         if exists(GALEON_BOOKMARKS_FILE):
  175.             parser = xml.sax.make_parser()
  176.             parser.setContentHandler(self)
  177.             
  178.             try:
  179.                 parser.parse(GALEON_BOOKMARKS_FILE)
  180.             except Exception:
  181.                 e = None
  182.                 print 'Failed to parse galeon bookmarks, this is a know bug #338762:', e
  183.                 traceback.print_exc()
  184.             except:
  185.                 None<EXCEPTION MATCH>Exception
  186.             
  187.  
  188.         None<EXCEPTION MATCH>Exception
  189.  
  190.     
  191.     def characters(self, chars):
  192.         self.chars = self.chars + chars
  193.  
  194.     
  195.     def startElement(self, name, attrs):
  196.         self.chars = ''
  197.         if name == 'bookmark':
  198.             self.title = None
  199.             self.href = attrs['href'].encode('latin1')
  200.             self.smarthref = None
  201.             self.nick = None
  202.         
  203.  
  204.     
  205.     def endElement(self, name):
  206.         if name == 'title':
  207.             self.title = self.chars.encode('utf8')
  208.         elif name == 'smarturl':
  209.             self.smarthref = self.chars.encode('latin1')
  210.             self.smarthref = re.sub('{.*}', '', self.smarthref)
  211.         elif name == 'nick':
  212.             self.nick = self.chars.encode('latin1')
  213.         elif name == 'bookmark':
  214.             if self.href.startswith('javascript:'):
  215.                 return None
  216.             
  217.             img = None
  218.             host = get_url_host(self.href)
  219.             if host in self._cache:
  220.                 img = self._cache[host]
  221.             
  222.             bookmark = BrowserMatch(self.handler, self.title, self.href, icon = img)
  223.             self._indexer.add('%s %s' % (self.title, self.href), bookmark)
  224.             if self.nick != None and self.smarthref != None:
  225.                 quicksearch = BrowserSmartMatch(self.handler, self.title, self.smarthref, icon = img, bookmark = bookmark, prefix_to_strip = self.nick)
  226.                 self._qsindexer[self.nick] = quicksearch
  227.             
  228.             if self.smarthref != None:
  229.                 bookmark = BrowserSmartMatch(self.handler, self.title, self.smarthref, icon = img, bookmark = bookmark)
  230.                 self._smart_bookmarks.append(bookmark)
  231.             
  232.         
  233.  
  234.  
  235.  
  236. class GaleonFaviconCacheParser(xml.sax.ContentHandler):
  237.     
  238.     def __init__(self):
  239.         xml.sax.ContentHandler.__init__(self)
  240.         self.galeon_dir = expanduser('~/.galeon/')
  241.         self.filename = join(self.galeon_dir, 'favicon_cache.xml')
  242.         self.cache = None
  243.         self.chars = ''
  244.         self.url = None
  245.         self.name = None
  246.  
  247.     
  248.     def get_cache(self):
  249.         '''
  250. \t\tReturns a dictionary of (host, favicon path) entries where
  251. \t\t  host is the hostname, like google.com (without www)
  252. \t\t  favicon path is the on-disk path to the favicon image file.
  253. \t\t'''
  254.         if self.cache != None:
  255.             return self.cache
  256.         
  257.         self.cache = { }
  258.         if exists(self.filename):
  259.             parser = xml.sax.make_parser()
  260.             parser.setContentHandler(self)
  261.             parser.parse(self.filename)
  262.         
  263.         return self.cache
  264.  
  265.     
  266.     def characters(self, chars):
  267.         self.chars = self.chars + chars
  268.  
  269.     
  270.     def startElement(self, name, attrs):
  271.         self.chars = ''
  272.         if name == 'entry':
  273.             self.url = attrs['url']
  274.             self.name = attrs['favicon']
  275.         
  276.  
  277.     
  278.     def endElement(self, name):
  279.         if name == 'entry':
  280.             host = get_url_host(self.url)
  281.             self.cache[host] = join(self.galeon_dir, 'favicon_cache', self.name.encode('utf8'))
  282.         
  283.  
  284.  
  285.  
  286. class GaleonHistoryParser(xml.sax.ContentHandler):
  287.     
  288.     def __init__(self, handler, cache):
  289.         xml.sax.ContentHandler.__init__(self)
  290.         self.handler = handler
  291.         self._cache = cache
  292.         self._indexer = deskbar.Indexer.Indexer()
  293.         self._index_history()
  294.  
  295.     
  296.     def get_indexer(self):
  297.         '''
  298. \t\tReturns a completed indexer with the contents of the history file
  299. \t\t'''
  300.         return self._indexer
  301.  
  302.     
  303.     def _index_history(self):
  304.         if exists(GALEON_HISTORY_FILE):
  305.             parser = xml.sax.make_parser()
  306.             parser.setContentHandler(self)
  307.             parser.parse(GALEON_HISTORY_FILE)
  308.         
  309.  
  310.     
  311.     def startElement(self, name, attrs):
  312.         self.chars = ''
  313.         if name == 'item':
  314.             url = attrs['url'].encode('utf8')
  315.             title = attrs['title'].encode('utf8')
  316.             pixbuf = None
  317.             
  318.             try:
  319.                 host = get_url_host(url)
  320.                 if host in self._cache:
  321.                     pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(self._cache[host], deskbar.ICON_HEIGHT, deskbar.ICON_HEIGHT)
  322.             except Exception:
  323.                 msg = None
  324.                 print 'Error:endElement(%s):Title:%s:%s' % (name.encode('utf8'), title, msg)
  325.  
  326.             item = BrowserMatch(self.handler, title, url, True, icon = pixbuf)
  327.             self._indexer.add('%s %s' % (title, url), item)
  328.         
  329.  
  330.     
  331.     def characters(self, chars):
  332.         pass
  333.  
  334.     
  335.     def endElement(self, name):
  336.         pass
  337.  
  338.  
  339.